package ${package}.controller;

import ${package}.pojo.${entity};
import ${package}.service.I${entity}Service;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import ${package}.service.I${entity}Service;
import io.swagger.annotations.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import com.hifar.sys.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@RestController
@RequestMapping("/api/busi/${prefix}/${entity?uncap_first}Controller")
@Api(tags = "${table.comment}功能")
public class ${entity}Controller {

    @Autowired
    private I${entity}Service ${entity?uncap_first}Service;

    @PostMapping(value = "/list")
    @ApiOperation(value = "${table.comment}功能:分页查询列表")
    public BusiResponse list(HttpServletRequest request) {
        Map<String,Object> pageInfo = RequestConver.getPageInfo(request);
        QueryWrapper<${entity}> queryWrapper = RequestConver.getPostParam(pageInfo);
        queryWrapper.orderByDesc("create_time");
        if (queryWrapper != null && pageInfo.size() > 0) {
             Page<${entity}> page = new Page<>((Integer) pageInfo.get("pageNo"), (Integer)pageInfo.get("pageSize"));
             Page<${entity}> page1 = ${entity?uncap_first}Service.page(page, queryWrapper);
             /*计算分页信息返回给前端*/
             PageResult pageResult = new PageResult();
             pageResult.setData(page1.getRecords());
             pageResult.setTotalCount(page1.getTotal());
             pageResult.setPageNo(page.getCurrent());
             pageResult.setPageSize(page1.getSize());
             double toPage = page1.getTotal()/(double)page1.getSize();
             pageResult.setTotalPage((int)Math.ceil(toPage));
             return BusiResponse.OK(pageResult);
        }else {
             return BusiResponse.error("参数不正确！");
        }
    }

    @PostMapping(value = "/listAll")
    @ApiOperation(value = "${table.comment}功能:查询列表(不带分页)")
    public BusiResponse listAll(HttpServletRequest request) {
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        List<${entity}> list = ${entity?uncap_first}Service.list(queryWrapper);
        if (list != null && list.size() > 0) {
            return BusiResponse.OK(list);
        }else {
            return BusiResponse.ok("没有查询到数据！");
        }
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "${table.comment}功能:新增")
    public BusiResponse add(@RequestBody ${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}Service.save(${entity?uncap_first});
        return BusiResponse.ok("添加成功");
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "${table.comment}功能:编辑")
    public BusiResponse edit(@RequestBody ${entity} ${entity?uncap_first}) {
        ${entity?uncap_first}Service.updateById(${entity?uncap_first});
        return BusiResponse.ok("编辑成功！");
    }


    @PostMapping("/delete")
    @ApiOperation(value = "${table.comment}功能:逻辑删除")
    public BusiResponse delete(@RequestBody List<String> idList){
        QueryWrapper<${entity}> delete = new QueryWrapper<>();
        delete.in("id",idList);
        ${entity?uncap_first}Service.remove(delete);
        return BusiResponse.ok("删除成功！");
    }


    @GetMapping("/queryById")
    @ApiOperation(value = "${table.comment}功能:根据id查询数据")
    public BusiResponse queryById(@RequestParam(value = "id") String id){
        ${entity} byId = ${entity?uncap_first}Service.getById(id);
        return BusiResponse.OK(byId);
    }


}
